msdn说: DisplayMember is set to the comboBox control, it uses the default ToString of the object,
public class Person{ public int Id { get; set; } public string Name { get; set; } public string Lastname { get; set; } public override string ToString() { return String.Format("{0} {1}", Name, Lastname); }}private void button2_Click(object sender, EventArgs e){ var people = LoadSamplePeople();//.DisplayMember is set to the comboBox control, it uses the default ToString of the object,comboBox1.ValueMember = "Id"; comboBox1.DataSource = people;}private IEnumerable<Person> LoadSamplePeople(){ return new List<Person>() { new Person(){ Id = 1, Name = "Sergio", Lastname = "Tapia" }, new Person(){ Id = 2, Name = "Daniel", Lastname = "Tapia" } };}
评论